package com.lzh.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author zhehen.lu
 * @date 2025/8/13 19:58
 */
public class JacksonBigDecimalFloor2BitPercentSerializer extends JsonSerializer<BigDecimal> {
    public JacksonBigDecimalFloor2BitPercentSerializer() {
    }

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (bigDecimal == null) {
            jsonGenerator.writeString("0.00%");
        } else {
            BigDecimal decimal = bigDecimal.setScale(2, RoundingMode.FLOOR);
            jsonGenerator.writeString(decimal.toString() + "%");
        }

    }
}
